<template>
  <div v-if="total > 0" class="container">
    <!-- 复选框 -->
    <el-checkbox v-model="showCover" style="margin:15px 0px;">是否显示封面</el-checkbox>

    <!-- 表格 -->
    <el-table :data="books" border style="width: 100%">
      <el-table-column prop="title" label="书名" width="250" fixed align="center" />
      <el-table-column prop="creator" label="作者" width="150" align="center" />
      <el-table-column prop="publisher" label="出版社" width="150" align="center" />
      <el-table-column prop="language" label="语言" width="150" align="center" />
      <el-table-column prop="name" label="文件名" width="150" align="center" />
      <el-table-column v-if="showCover" label="封面" width="150" align="center">
        <template slot-scope="{row}">
          <img :src="row.coverURL" width="80px">
        </template>
      </el-table-column>
      <el-table-column prop="rootUrl" label="根路径" width="150" align="center" />
      <el-table-column prop="url" label="文件路径" width="350" align="center" />
      <el-table-column prop="unzipURL" label="解压路径" width="350" align="center" />

      <el-table-column
        fixed="right"
        label="操作"
        width="100"
      >
        <template slot-scope="{row}">
          <div v-permission="['admin']">
            <el-button type="text" size="small" @click="delBook(row)">删除</el-button>
            <el-button type="text" size="small" @click="editBook(row)">修改</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      style="text-align: center;margin-top:10px"
      :current-page="pageNo"
      :page-sizes="[2,5,10,20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import { delBook } from '@/api/book'
import { mapState } from 'vuex'
import permission from '@/directive/permission/index.js'
export default {
  name: 'BooksList',
  directives: { permission },
  data() {
    return {
      pageSize: 2,
      pageNo: 1,
      showCover: false
    }
  },
  computed: {
    ...mapState({
      books: state => state.book.books,
      total: state => state.book.total
    })
  },
  async created() {
    await this.init()
  },
  methods: {
    async editBook({ _id }) {
      await this.$router.push(`/book/edit/${_id}`)
      // this.$bus.$emit("sendId",_id)
    },
    async delBook({ _id, title }) {
      try {
        // 提示信息中显示title
        const info = `是否要删除<strong style="color:red">${title}</strong>?`
        const res = await this.$confirm(info, '提示', {
          dangerouslyUseHTMLString: true,
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        if (res === 'confirm') {
          // 发请求删除数据  需要id
          try {
            const { code } = await delBook(_id)
            if (code === 20000) {
              this.$message.info('删除数据成功')
              // 删除成功 重新刷新页面
              this.init()
              // this.push("/book/list") ??
            }
          } catch (error) {
            this.$message.error('删除数据失败')
          }
        }
      } catch (error) {
        this.$message({
          type: 'info',
          message: '已取消删除',
          duration: 500
        })
      }
    },
    async handleSizeChange(pageSize) {
      this.pageSize = pageSize
      await this.init()
    },
    async handleCurrentChange(pageNo) {
      this.pageNo = pageNo
      await this.init()
    },
    async init() {
      await this.$store.dispatch('book/getTotal')
      await this.$store.dispatch('book/getBooks', {
        pageSize: this.pageSize,
        pageNo: this.pageNo
      })
    }
  }
}
</script>

<style scoped>
.chart-container{
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 100px;
}
</style>

